package com.symaster.common.swing.config;

import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yinmiao
 * @date 2021-10-20 10:06
 */
public class AppConfig {

    public static final String AUXILIARY_BACKGROUND_COLOR = "auxiliary_background_color"; // 辅助景色
    public static final String MAIN_FONT_COLOR = "main_font_color"; // 主要字体颜色
    public static final String AUXILIARY_FONT_COLOR = "auxiliary_font_color"; // 辅助字体颜色
    public static final String MAIN_BACKGROUND_COLOR = "main_background_color"; // 主背景色
    public static final String SUB_BACKGROUND_COLOR = "sub_background_color"; // 次要背景颜色
    public static final String DEEP_BACKGROUND = "deep_background"; // 深色背景
    public static final String SELECT_COLOR = "select_color"; /// 选中颜色
    public static final String HIGH_GLOSS_COLOR = "high_gloss_color"; // 高光颜色
    public static final String ERROR_COLOR = "error_color"; // 错误颜色
    public static final String HIGH_ERROR_COLOR = "high_error_color"; // 高光颜色
    private static Map<String, String> applicationProperties;

    static {
        initConfig();
    }

    public static String getProperty(String key) {
        return applicationProperties.get(key);
    }

    public static Color getColor(String key) {
        String s = applicationProperties.get(key);
        if (StringUtils.isEmpty(s)) {
            return null;
        }
        String[] s1 = s.replace(" ", "").split(",");
        if (s1.length < 3) {
            return null;
        }
        return new Color(Integer.parseInt(s1[0]), Integer.parseInt(s1[1]), Integer.parseInt(s1[2]));
    }

    public static Color getColorOrDefault(String key, Color defaultColor) {
        Color color = getColor(key);
        return color == null ? defaultColor : color;
    }

    public static void setColor(String key, Color value) {
        applicationProperties.put(key, String.format("%s,%s,%s", value.getRed(), value.getGreen(), value.getBlue()));
    }

    public static void setColor(String key, String value) {
        applicationProperties.put(key, value);
    }

    public static void setProperty(String key, String value) {
        applicationProperties.put(key, value);
    }

    public static void remove(String key) {
        applicationProperties.remove(key);
    }

    private static void initConfig() {
        if (applicationProperties == null) {
            applicationProperties = new HashMap<>();
        }

        // 53, 59, 67
        setColor(AUXILIARY_BACKGROUND_COLOR, new Color(53, 59, 67)); // 辅助景色
        setColor(MAIN_FONT_COLOR, new Color(221, 242, 255)); // 主要字体颜色
        setColor(AUXILIARY_FONT_COLOR, new Color(193, 209, 222)); // 辅助字体颜色
        setColor(MAIN_BACKGROUND_COLOR, new Color(36, 41, 46)); // 主背景色
        setColor(SUB_BACKGROUND_COLOR, new Color(30, 36, 40)); // 次要背景颜色
        setColor(DEEP_BACKGROUND, new Color(24, 27, 32)); // 深色背景
        setColor(SELECT_COLOR, new Color(128, 200, 100)); // 选中颜色
        setColor(HIGH_GLOSS_COLOR, new Color(249, 130, 108)); // 高光颜色
        setColor(ERROR_COLOR, new Color(243, 91, 75)); // 错误颜色
        setColor(HIGH_ERROR_COLOR, new Color(246, 116, 130)); // 高光颜色
    }
}
